<template>
  <div class="login">
    <div class="mask" @click="$store.commit('ShowOrHideLogin', false)"></div>
    <div class="login-box">
      <div class="close" @click="$store.commit('ShowOrHideLogin', false)">
        <img src="../../assets/images/login/userArrow.png" alt="" />
      </div>
      <ul class="login-tab flex">
        <li :class="{ active: currentIndex === 0 }" @click="currentIndex = 0">
          手机号码登录
        </li>
        <li :class="{ active: currentIndex === 1 }" @click="weixinClickFn">
          微信扫码登录
        </li>
      </ul>
      <div v-show="currentIndex === 0" class="login-form">
        <div class="item">
          <input type="text" placeholder="请输入手机号码" v-model="phone" />
        </div>
        <div class="item huakuai">
          <slide-verify
            v-model="ishuakuai"
            :l="42"
            :r="20"
            :w="395"
            :h="135"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
            :style="{ width: '100%' }"
            class="slide-box"
            ref="slideBlock"
            :slider-text="msg"
          ></slide-verify>
          <span class="success" v-show="ishuakuai">{{ msg }}</span>
        </div>
        <div class="item">
          <input
            type="text"
            placeholder="请输入短信验证码"
            v-model.trim="code"
          />
          <div class="btn" @click="Verification()">
            获取验证码<span v-show="time != 60">({{ time }})</span>
          </div>
        </div>
        <div class="item">
          <div class="btn btn-lg" @click="phoneLogin()">登录</div>
        </div>
      </div>
      <div v-show="currentIndex === 1" class="weixin">
        <div id="weixin"></div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { validateTelephone } from "../../utils/index";
import { toast } from "../../utils/toast";
import { getVerify, phoneLoginApi } from "../../http/api";
export default {
  data() {
    return {
      // 判断类名是否显示
      currentIndex: 0,
      msg: "向右滑动",
      time: 60,
      phone: "",
      // 判断时间是否显示
      // isVerification:false,
      // 判断图片验证是否成功
      ishuakuai: false,
      code: "",
    };
  },
  methods: {
    // 点击了微信扫码登录
    weixinClickFn() {
      this.currentIndex = 1;
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa(process.env.VUE_APP_STATE_URL + "/#" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLCAuaW1wb3dlckJveCAuaW5mb3sNCiAgICBkaXNwbGF5OiBub25lOw0KfQ0KDQouaW1wb3dlckJveCAucXJjb2Rlew0KICAgIG1hcmdpbi10b3A6IDIwcHg7DQp9DQoNCg==",
      });
    },
    // 登录
    async phoneLogin() {
      if (!validateTelephone(this.phone))
        return toast("请输入正确的手机号", "denger");

      if (this.ishuakuai === false) return toast("请拖动图片验证", "denger");

      if (this.code === "") return toast("请输入验证码", "denger");
      // 发请求
      const res = await phoneLoginApi({ verifyCode: this.code, phone: this.phone });
      if (res.code === 0) {
        console.log(res["x-auth-token"]);
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 提示
        toast(res.message, "success");
        // 把login隐藏
        this.$store.commit("ShowOrHideLogin", false);
      }
    },
    Verification() {
      if (!validateTelephone(this.phone))
        return toast("请输入正确的手机号", "denger");

      if (this.ishuakuai === false) return toast("请拖动图片验证", "denger");

      if (this.time != 60) return toast("验证码已发送，请勿重复点击", "warning");
      toast("验证码发送成功", "success");

      this.time--;
      let timer = setInterval(() => {
        this.time--;
        if (this.time == 0) {
          this.time = 60;
          clearInterval(timer);
        }
      }, 1000);

      getVerify(this.phone).then((res) => {
        if (res.code === 0) {
          console.log(res);
        }
      });
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      // this.msg = "login success, 耗时 " + ms + "s";
      this.msg = "验证成功, 耗时 " + ms + "s";
      this.ishuakuai = true;
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
      this.ishuakuai = false;
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 点击登录按钮
    // submitFn(formName) {
    //   if (this.msg == "再试一次" || this.msg == "向右滑动") {
    //     console.log("请滑动拼图");
    //   } else {
    //     console.log("开始登录");
    //   }
    // },
  },
};
</script>
 
<style lang = "less" scoped>
.login {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  .mask {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
  }
  .login-box {
    width: 400px;
    height: 423px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: url("../../assets/images/login/loginBg.png") no-repeat;
    padding: 40px 80px;
    .login-tab {
      width: 70%;
      margin: 20px auto;
      li {
        cursor: pointer;
        padding: 0 10px;
        color: #ccc;
        &.active {
          color: #333;
        }
        &:nth-child(2) {
          border-left: 1px solid #333;
        }
      }
    }
    .close {
      cursor: pointer;
      position: absolute;
      top: 20px;
      right: 70px;
    }
    .login-form {
      .item {
        margin-top: 20px;
        display: flex;
        input {
          flex: 1;
          height: 40px;
          outline: none;
          padding: 0 10px;
          border: 1px solid #ccc;
        }
        .btn {
          cursor: pointer;
          width: 120px;
          background-color: #0a328e;
          text-align: center;
          line-height: 40px;
          color: #fff;
          margin-left: 10px;
          &.btn-lg {
            margin: 0;
            flex: 1;
          }
        }
      }
    }
    .weixin {
      display: flex;
      justify-content: space-around;
    }
  }
}
.huakuai {
  position: relative;
}
.success {
  position: absolute;
  left: 50%;
  bottom: 5px;
  transform: translate(-50%, -50%);
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -140px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -140px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>